<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <title>ჩატი</title>
    <style type="text/css">
        @font-face {
        	font-family: "BPG Arial";
        	src:	url("../../media/fonts/bpg_arial.eot");
        	src:	url("../../media/fonts/bpg_arial.eot?#iefix") format("embedded-opentype"),
        			url("../../media/fonts/bpg_arial.ttf") format("truetype"),
        			url("../../media/fonts/bpg_arial.svg#JournalRegular") format("svg");
        }
        body{
        	font-size: 12px;
	        font-family: BPG arial;
        	margin: 0;
        	padding: 0;
        }
        #chat_wrapper{
        	font-size: 13.5px;
        	font-family: BPG arial;
        	width: 348px;
        	height: 448px;
        	border: 1px solid green;
        	padding: 5px;
        	border-radius: 3.5px;
        }
        #guest_process_text{
        	width: 254px;
        	height: 240px;
        	border: 1px solid green;
        	overflow-y: scroll;
        	font-size: 11.5px;
        	font-family: BPG arial;
        }
        #guest_send_text{
        	margin-top: 5px;
        	width: 250px;
        	height:50px;
        	resize: none;
        }
        
        .chat_head{
        	display: block;
        	width: 250px;
        	background: #86B520;
        	border: 1px solid green;
        	padding: 2px;
        	float: left;
        	margin-bottom: 2px;
        }
        
        .chat_head span{
        	display: block;
        	float: left;
        	color: white;
        	margin-top: 2px;
        	cursor: pointer;
        }
        
        .chat_head button{   
        	border: none;     	
	        float: right;
        	background: #86B520;
        	color: white;
        	cursor: pointer;
        }
        
        input, select{
        	background-color: #FFFFFF;
            border: 1px solid #CCCCCC;
        	height: 25px;
            padding: 4px 6px;
            margin-bottom: 10px;
            color: #555555;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 5px;
        	font-size: 12px;
            font-weight: normal;
            line-height: 25px;
        	font: -webkit-small-control;
            color: initial;
            letter-spacing: normal;
            word-spacing: normal;
            text-transform: none;
            text-indent: 0px;
            text-shadow: none;
            display: inline-block;
            text-align: start;
        	-webkit-rtl-ordering: logical;
            -webkit-user-select: text;
            cursor: auto;
        	width: 325px;
        	font-family: BPG arial;
        }
        
        label{
        	display: block;
            margin-bottom: 10px;
        	font-size: 14px;
            font-weight: normal;
            line-height: 20px;
        }
        
        #send{
        	background-color: #64B10E;
            color: #FFFFFF;
        	width: 75px;
        	height: 30px;
        }
        
        form{
        	padding: 5px;
        }
        
        #chat_body_process{
        	display: block;
        	padding: 2px 0;
        }
    </style>
    
    <script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../../js/jquery-ui-1.10.3.custom.min.js"></script>
	
    <script type="text/javascript">
        var aJaxURL	= "../../server-side/chat_live/chat.action.php";		//server side folder url
    	var tName	= "all_chat";										//table name
    	var fName	= "chat_dialog";									//form name
    	
        $(document).ready(function () {
    		<?php 
        		if((isset($_POST['send']) && !empty($_POST['guest_name']) && !empty($_POST['guest_email']) && !empty($_POST['guest_text'])) || isset($_SESSION['chat_session_id'])){
        		  echo 'timer();';
        		}
    		?>
    		
    		
    	});
    	
        function scroll_bottom(body_id, ccc){

        	if(ccc == 1){
    		var bottom = document.getElementById(body_id);

    		bottom.scrollTop = bottom.scrollHeight;
        	}
    	}  
        
        function checker(ccc){
        	param 			= new Object();        	
            param.act    = 'message';
			param.id     = $("#guest_id").val();
			
    		$.ajax({
    			type: "POST",
		        url: aJaxURL,
			    data: param,
			    dataType: "json",
		        success: function(data) {
					$("#guest_process_text").html(data.message);
				    scroll_bottom('guest_process_text', ccc);
			    }
		    });
        }
        
        function timer() {
			setTimeout(function() {
				checker();
				timer();
				}, 1000);
		}

        $(document).on("keydown", "#guest_send_text", function(event) {
        	if (event.keyCode == $.ui.keyCode.ENTER) {
        		var id_1 = $("#guest_id").val();
        		var text_1 = $(this).val();
        		param 	= new Object();
        		param.act         = 'guest_chat';
    			param.id_add_guest      = id_1;
    			param.text_add_guest    = text_1;

        		$.ajax({
        			type: "POST",
    		        url: aJaxURL,
    			    data: param,
    			    dataType: "json",
    		        success: function(data) {
        		        $("#guest_send_text").val('');
        		        checker(1);
    			    }
    		    });
    		    
        	}
    	});       

        function close_chat(){
        	var id = $("#guest_id").val();
        	        		
        	param 	= new Object();
    		param.act         = 'close_chat';
			param.close_id    = id;

    		$.ajax({
    			type: "POST",
		        url: aJaxURL,
			    data: param,
			    dataType: "json",
		        success: function(data) {
		        	$("#guest_id").val('');
		        	$("#guest_process_text").html('');
		        	window.location.replace('http://212.72.155.175:8181/client-side/chat_live/chat_process.php');
			    }
		    });
        }

    </script>
    </head>

    <body>
    
        <div id="chat_wrapper">
            <?php          
            
            if(isset($_POST['send']) && !empty($_POST['guest_name']) && !empty($_POST['guest_email']) && !empty($_POST['guest_text'])){
                mysql_connect('localhost','root','Gl-1114') or die('servertan kavshiri araa');
                mysql_select_db('asteriskcdrdb') or die('baza ar moidzebna');                              
                
                function ip()
                {
                    global $REMOTE_ADDR;
                    global $HTTP_X_FORWARDED_FOR, $HTTP_X_FORWARDED, $HTTP_FORWARDED_FOR, $HTTP_FORWARDED;
                    global $HTTP_VIA, $HTTP_X_COMING_FROM, $HTTP_COMING_FROM;
                    if (empty($REMOTE_ADDR)) {
                        if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) {
                            $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['REMOTE_ADDR'])) {
                            $REMOTE_ADDR = $_ENV['REMOTE_ADDR'];
                        }
                        else if (@getenv('REMOTE_ADDR')) {
                            $REMOTE_ADDR = getenv('REMOTE_ADDR');
                        }
                    }
                    if (empty($HTTP_X_FORWARDED_FOR)) {
                        if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                            $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
                            $HTTP_X_FORWARDED_FOR = $_ENV['HTTP_X_FORWARDED_FOR'];
                        }
                        else if (@getenv('HTTP_X_FORWARDED_FOR')) {
                            $HTTP_X_FORWARDED_FOR = getenv('HTTP_X_FORWARDED_FOR');
                        }
                    }
                    if (empty($HTTP_X_FORWARDED)) {
                        if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
                            $HTTP_X_FORWARDED = $_SERVER['HTTP_X_FORWARDED'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED'])) {
                            $HTTP_X_FORWARDED = $_ENV['HTTP_X_FORWARDED'];
                        }
                        else if (@getenv('HTTP_X_FORWARDED')) {
                            $HTTP_X_FORWARDED = getenv('HTTP_X_FORWARDED');
                        }
                    }
                    if (empty($HTTP_FORWARDED_FOR)) {
                        if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
                            $HTTP_FORWARDED_FOR = $_SERVER['HTTP_FORWARDED_FOR'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED_FOR'])) {
                            $HTTP_FORWARDED_FOR = $_ENV['HTTP_FORWARDED_FOR'];
                        }
                        else if (@getenv('HTTP_FORWARDED_FOR')) {
                            $HTTP_FORWARDED_FOR = getenv('HTTP_FORWARDED_FOR');
                        }
                    }
                    if (empty($HTTP_FORWARDED)) {
                        if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
                            $HTTP_FORWARDED = $_SERVER['HTTP_FORWARDED'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED'])) {
                            $HTTP_FORWARDED = $_ENV['HTTP_FORWARDED'];
                        }
                        else if (@getenv('HTTP_FORWARDED')) {
                            $HTTP_FORWARDED = getenv('HTTP_FORWARDED');
                        }
                    }
                    if (empty($HTTP_VIA)) {
                        if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
                            $HTTP_VIA = $_SERVER['HTTP_VIA'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['HTTP_VIA'])) {
                            $HTTP_VIA = $_ENV['HTTP_VIA'];
                        }
                        else if (@getenv('HTTP_VIA')) {
                            $HTTP_VIA = getenv('HTTP_VIA');
                        }
                    }
                    if (empty($HTTP_X_COMING_FROM)) {
                        if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
                            $HTTP_X_COMING_FROM = $_SERVER['HTTP_X_COMING_FROM'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['HTTP_X_COMING_FROM'])) {
                            $HTTP_X_COMING_FROM = $_ENV['HTTP_X_COMING_FROM'];
                        }
                        else if (@getenv('HTTP_X_COMING_FROM')) {
                            $HTTP_X_COMING_FROM = getenv('HTTP_X_COMING_FROM');
                        }
                    }
                    if (empty($HTTP_COMING_FROM)) {
                        if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])) {
                            $HTTP_COMING_FROM = $_SERVER['HTTP_COMING_FROM'];
                        }
                        else if (!empty($_ENV) && isset($_ENV['HTTP_COMING_FROM'])) {
                            $HTTP_COMING_FROM = $_ENV['HTTP_COMING_FROM'];
                        }
                        else if (@getenv('HTTP_COMING_FROM')) {
                            $HTTP_COMING_FROM = getenv('HTTP_COMING_FROM');
                        }
                    }
                
                    if (!empty($REMOTE_ADDR)) {
                        $direct_ip = $REMOTE_ADDR;
                    }
                
                    $proxy_ip	 = '';
                    if (!empty($HTTP_X_FORWARDED_FOR)) {
                        $proxy_ip = $HTTP_X_FORWARDED_FOR;
                    } else if (!empty($HTTP_X_FORWARDED)) {
                        $proxy_ip = $HTTP_X_FORWARDED;
                    } else if (!empty($HTTP_FORWARDED_FOR)) {
                        $proxy_ip = $HTTP_FORWARDED_FOR;
                    } else if (!empty($HTTP_FORWARDED)) {
                        $proxy_ip = $HTTP_FORWARDED;
                    } else if (!empty($HTTP_VIA)) {
                        $proxy_ip = $HTTP_VIA;
                    } else if (!empty($HTTP_X_COMING_FROM)) {
                        $proxy_ip = $HTTP_X_COMING_FROM;
                    } else if (!empty($HTTP_COMING_FROM)) {
                        $proxy_ip = $HTTP_COMING_FROM;
                    }
                
                    if (empty($proxy_ip)) {
                        $ip =  $direct_ip;
                    } else {
                        $is_ip = preg_match('|^([0-9]{1,3}\.){3,3}[0-9]{1,3}|', $proxy_ip, $regs);
                        if ($is_ip && (count($regs) > 0)) {
                            $ip =  $regs[0];
                        } else {
                            $ip =  'unknow';
                        }
                    }
                    return $ip;
                }
                
                $guest_name         = mysql_real_escape_string($_POST['guest_name']);
                $guest_email        = mysql_real_escape_string($_POST['guest_email']);
                $guest_text         = mysql_real_escape_string($_POST['guest_text']);
                $guest_departament  = mysql_real_escape_string($_POST['guest_departament']);
                $guest_ip       = ip();
                
                mysql_query("INSERT INTO `chat` (`start_date`, `ip`, `email`, `name`, `departament_id`) VALUES (NOW(), '$guest_ip', '$guest_email', '$guest_name', '$guest_departament');");
                $hiden_id = mysql_fetch_assoc(mysql_query("SELECT id FROM `chat` WHERE DATE(start_date) = DATE(NOW()) AND ip = '$guest_ip' AND email = '$guest_email' AND `name` = '$guest_name' AND actived = 1 ORDER BY start_date DESC;"));
                $_SESSION['chat_session_id'] = $hiden_id[id];
                mysql_query("INSERT INTO `chat_detail` (`chat_id`, `date`, `message`, `type`) VALUES ('$_SESSION[chat_session_id]', NOW(), '$guest_text', '1');");
                
                 

                
            }
            if(isset($_SESSION['chat_session_id'])){
                $start_chat = '<input type="text" value="'.$_SESSION[chat_session_id].'" id="guest_id" style="display: none;"/>
                        <span class="chat_head"><span id="name_1">MoneyMan Chat</span> <button onclick="close_chat()">x</button> </span>
                        <div id="guest_process_text"></div>
                        <textarea id="guest_send_text" placeholder="შეიყვანეთ ტექსტი" required ></textarea>';
            }else {
                $start_chat =  '<form method="post" action="">
                            <span style="background: #86B520;color: #fff;height: 30px;display: block;position: absolute;width: 358px;top: 1px;left: 1px;padding-top: 10px;border-radius: 3.5px 3.5px 0 0;">ჩვენ ონლაინში ვართ, მოგვწერეთ!</span>
                            <br><br><p>მადლობთ რომ დაგვიკავშირდით, გთხოვთ შეავსოთ ქვემოთ მოცემული ფორმა.</p>
                            <label>სახელი</label>
                            <input type="text" name="guest_name" placeholder="სახელი" required/>
                            <label>ელ-ფოსტა</label>
                            <input type="email" name="guest_email" placeholder="ელ-ფოსტა" required/>
                            <label>დეპარტამენტი</label>
                            <select name="guest_departament"><option value="1">MoneyMan</option></select>
                            <label>შეტყობინება</label>
                            <input type="text" name="guest_text" placeholder="შეტყობინება" required />
                            <input type="submit" value="გაგზავნა" id="send" name="send"/>
                   </form>
                    ';
            }
            
            echo $start_chat;
            ?>
        </div>
    	
    </body>
</html>